package com.zhhs.common.utils.ftp;

import com.jcraft.jsch.*;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.util.Properties;

/**
 * @author peng
 * @version 1.0
 * @date 2023-07-27 10:55
 */
public class SftpUtil {
    /**
     * 获取一个sftp链接
     * @param host sftp服务器ip
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @return 返回ChannelSftp
     * @throws Exception 获取通道时的异常
     */
    public static ChannelSftp getSftpChannel(String host, Integer port, String username, String password) throws Exception{
        Session session;
        Channel channel = null;
        JSch jSch = new JSch();
        try {
            session = jSch.getSession(username, host, port);
            session.setPassword(password);

            // 配置链接的属性
            Properties properties = new Properties();
            properties.setProperty("StrictHostKeyChecking","no");
            session.setConfig(properties);

            // 进行sftp链接
            session.connect();

            // 获取通信通道
            channel = session.openChannel("sftp");
            channel.connect();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }
        return (ChannelSftp)channel;
    }

    /**
     * 上传文件
     * @param channelSftp sftp通道
     * @param localFile 本地文件
     * @param remoteFile 远程文件
     */
    public static void upload(ChannelSftp channelSftp, String localFile, String remoteFile){
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(localFile);
            channelSftp.put(inputStream, remoteFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null){
                try{
                    inputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从sftp服务器上下载文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     * @param localFile 本地文件
     */
    public static void download(ChannelSftp channelSftp, String remoteFile, String localFile){
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(localFile);
            channelSftp.get(remoteFile, outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null){
                try{
                    outputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     */
    public static void deleteFile(ChannelSftp channelSftp, String remoteFile) throws Exception{
        try {
            channelSftp.rm(remoteFile);
        } catch (SftpException e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 关闭sftp链接
     * @param channelSftp sftp通道
     * @throws Exception 关闭session的异常
     */
    public static void closeSession(ChannelSftp channelSftp) throws Exception{
        if(channelSftp == null){
            return;
        }

        Session session = null;
        try {
            session = channelSftp.getSession();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }finally {
            if(session != null){
                session.disconnect();
            }
        }

    }

    public static void main(String[] args) throws Exception {
        ChannelSftp sftpClient =  getSftpChannel("39.105.71.11",23091,"zhhs_admin","eM1gDt#q2Zy4W69R");
        //上传
        //SftpUtil.upload(ftpClient,"E:\\test.txt","/data/中海测试.txt");
        //下载
        SftpUtil.download(sftpClient,"/data/中海测试.txt","E:\\大山测试111.txt");
        closeSession(sftpClient);
    }
}
